package com.link.play.fit.ai.ui.guide

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.google.android.play.core.review.ReviewException
import com.google.android.play.core.review.ReviewManagerFactory
import com.google.android.play.core.review.model.ReviewErrorCode
import com.link.play.fit.ai.R
import com.link.play.fit.ai.databinding.FgRateBinding


class RateFg : BasePageFragment<FgRateBinding>(FgRateBinding::inflate) {

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val appName = resources.getString(R.string.app_name)
        binding.tvLike.text = getString(R.string.rate_text_1, appName)
        binding.tvUsers.text = getString(R.string.rate_text_2, appName)
        binding.tvName1.text = "Tom Brown"
        binding.tv1.text =
            "I have been using the app for 2 months. I set my goal from 93 kg to be 87. And 2 months later I reach this goal. The app is helping me with the \"gamefication\" of the process of weight losing. Of course, It needs some improvements and I am looking forward to new updates soon. Hopefully"
        binding.tvName2.text = "Amy White"
        binding.tv2.text =
            "Excellent! Everything in the app is perfect, and makes tracking food much much easier than it is to do so manually. I am just curious when you will release the \"Log Workout\" feature? It would make the app way better for me after all, it's not just the food journaling and calculations that get automated, but also workouts! Keep it up! Cheers to the development team."


//        Google Play 会强制执行限时配额，规定系统向用户显示评价对话框的频率，因此不要频繁调用相关方法
//        startReviewFlow()
    }

    private fun startReviewFlow() {
        val manager = ReviewManagerFactory.create(requireContext())
        val request = manager.requestReviewFlow()
        request.addOnCompleteListener { task ->
            if (task.isSuccessful) {
                // 获取到 ReviewInfo 对象
                val reviewInfo = task.result
                // 启动应用内评价流程
                val flow = manager.launchReviewFlow(requireActivity(), reviewInfo)
                flow.addOnCompleteListener {
                    // 评价流程完成
                }
                flow.addOnFailureListener {
                    // 评价流程启动失败
                }
                flow.addOnSuccessListener {
                    // 评价流程启动成功
                }
                flow.addOnCanceledListener {
                    // 评价流程被取消
                }
            } else {
                // 请求 ReviewInfo 失败，处理错误
                @ReviewErrorCode val reviewErrorCode = (task.exception as ReviewException).errorCode
            }
        }
    }

}